# -*- coding: utf-8 -*-
import cgi
import wsgiref.handlers
from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
from google.appengine.ext.webapp import util
from google.appengine.ext import webapp
from model import *


def demo_sayHello():
	return 'Hello!'


def nuoqiu_CatPost(en,cn):
    cat = Category()
    cat.enname = en
    cat.cnname = cn
    cat.put()
    
def nuoqiu_StoryPost(struct):
    story = Story()
    story.name = struct['name']
    story.author = struct['author']
    story.createdate = struct['createdate']
    story.is_over = struct['is_over']
    story.put()
    
    

class XMLRPCDispatcher(SimpleXMLRPCDispatcher):
    def __init__(self, funcs):
        SimpleXMLRPCDispatcher.__init__(self, True, 'utf-8')
        self.funcs = funcs
        self.register_introspection_functions()

dispatcher = XMLRPCDispatcher({
    'demo.sayHello': demo_sayHello,
    'nuoqiu.CatPost': nuoqiu_CatPost,
    'nuoqiu.StoryPost': nuoqiu_StoryPost,
})

class RpcHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('<h1>please use POST</h1>')
    
    def post(self):
        response = dispatcher._marshaled_dispatch(self.request.body)
        self.response.out.write(response)


application = webapp.WSGIApplication([
  ('/rpc', RpcHandler),
], debug=True)


def main():
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
    main()

